﻿using com.iot.core.exception;
using com.iot.core.infrastructure;
using System.Net.Http.Headers;
using System.Text;
using System.Web;

namespace com.iot.ht.Controllers.htsoft.service
{
    public class SmsRyd : Sms
    {
        string AppID { get; set; } = "";
        string AppKey { get; set; } = "";
        string SignName { get; set; } = "";
        string Url { get; set; } = "";       
        void Cfg()
        {
            var arr = SmsCfg.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (arr.Length != 4)
            {
                throw new MyException("smsRyd配置错误");
            }
            AppID = arr[0].Trim();
            AppKey = arr[1].Trim();
            SignName = arr[2].Trim();
            Url = arr[3].Trim();
        }

        public static string MD5(string Sourcein)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(Sourcein);
            byte[] MD5Out = MD5CSP.ComputeHash(MD5Source);
            return ToHexString(MD5Out);
            //return Encoding.ASCII.GetString(MD5Out);    
        }
        public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
        {
            string hexString = string.Empty;

            if (bytes != null)
            {

                StringBuilder strB = new StringBuilder();

                for (int i = 0; i < bytes.Length; i++)
                {

                    strB.Append(bytes[i].ToString("X2"));

                }

                hexString = strB.ToString();

            }
            return hexString;

        }


        public override async Task<string> SendAsync(string Mobiles, List<string> lstMsg)
        {
            if (string.IsNullOrEmpty(Mobiles))
            {
                return "手机号为空";
            }
            if (string.IsNullOrEmpty(AppID))
            {
                Cfg();
            }
            Encoding encoding = Encoding.UTF8;
            var data = lstMsg[0];
            string appid =  AppID;
            string appkey = AppKey;
            string msg = SignName + data;
            

            string s = appid + Mobiles + msg + appkey;
            string sign = MD5(s).ToLower();



            string postData = "appid=" + appid + "&mobile=" + Mobiles + "&msg=" + HttpUtility.UrlEncode(msg) + "&sign=" + sign;           
            byte[] bs = encoding.GetBytes(postData);

            var content = new ByteArrayContent(bs);
            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
             
            var _httpClientFactory = EngineContext.Current.Resolve<IHttpClientFactory>();
            var httpClient = _httpClientFactory.CreateClient();
            httpClient.Timeout = new TimeSpan(0, 0, 3); 
            var response = await httpClient.PostAsync(Url+ "/http/send", content);
            return await response.Content.ReadAsStringAsync();
        }
    }
}
